package com.janlei.tdd.lintcode.t_37;

import java.util.function.IntConsumer;
import java.util.stream.IntStream;


public class T_37 {
    /**
     * @param number: A 3-digit number.
     * @return: Reversed number.
     */
    public int reverseInteger(int number) {
        // write your code here
        checkBorder(number);
        String reverser = reverseByString(String.valueOf(number));
        return Integer.parseInt(reverser);
    }

    private String reverseByString(String content) {
        int len = content.length() - 1;
        StringBuilder builder = new StringBuilder();

        IntConsumer consumer = index -> builder.append(content.charAt(len - index));
        IntStream.rangeClosed(0, len)
                .forEachOrdered(consumer);
        return builder.toString();
    }

    private void checkBorder(int number) {
        boolean illegal = number > 999 || number < 100;

        if (illegal) {
            throw new IllegalArgumentException("参数非法，不在范围内！");
        }
    }
}
